var e = require("../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../@babel/runtime/helpers/defineProperty");

require("../../@babel/runtime/helpers/Arrayincludes");

var a = e(require("../../api/api_queryRemain.js")), o = (getApp(), [ {
    isFocus: !0,
    value: ""
}, {
    isFocus: !1,
    value: ""
}, {
    isFocus: !1,
    value: ""
}, {
    isFocus: !1,
    value: ""
}, {
    isFocus: !1,
    value: ""
}, {
    isFocus: !1,
    value: ""
} ]);

Page({
    data: {
        showIconInfo: !1,
        apiRes: {},
        showLoading: !1,
        authToken: "",
        encryptPhone: "",
        userInfo: {},
        isLogin: !1,
        showModal: !1,
        autoFocues: !1,
        countDown: 0,
        timer: null,
        inputValue: "",
        showLoadIcon: !0,
        inputList: JSON.parse(JSON.stringify(o))
    },
    onLoad: function(e) {
        this.setData({
            authToken: e.authToken || "",
            encryptPhone: e.encryptPhone || ""
        }), this.getDataFromStorage();
    },
    getDataFromStorage: function() {
        var e = this, t = this.checkExpired();
        console.log("是否过期", t), t ? e.getQQqueryRemain() : wx.getStorage({
            key: "code",
            success: function(t) {
                console.log("成功从缓存取出code=", t.data), e.beforeGetQQqueryRemain(t.data), e.setData({
                    "apiRes.code": t.data
                });
            },
            fail: function() {
                console.log("没有缓存"), e.getQQqueryRemain();
            }
        });
    },
    checkExpired: function() {
        try {
            var e = wx.getStorageSync("timestamp");
            if (!e) return console.log("缓存没有查询到时间戳，默认过期"), !0;
            var t = Date.parse(new Date()), a = t - Number(e) > 6e4;
            return console.log("计算时间差", t - Number(e)), a && (wx.removeStorageSync("code"), 
            wx.removeStorageSync("timestamp"), console.log("清除code和timestamp成功")), a;
        } catch (e) {
            e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
            return !0;
        }
    },
    clickBtn: function() {
        this.beforeGetQQqueryRemain(this.data.apiRes.code);
    },
    beforeGetQQqueryRemain: function(e) {
        this.setData({
            showModal: !0
        });
        [ "0005", "0006", "0012", "0013", "0014", "0009" ].includes(e) || this.getQQqueryRemain();
    },
    putIntoStorage: function(e) {
        if ([ "0005", "0006", "0012", "0013", "0014", "0009" ].includes(e.code)) if (wx.getStorageSync("timestamp")) console.log("查询到已经存在时间戳，不需要重复存入时间戳"); else {
            var t = Date.parse(new Date());
            console.log("存入时间戳", t), wx.setStorage({
                key: "timestamp",
                data: t
            });
        } else console.log("查询到不是异常code列表，不需要存入时间戳");
        wx.setStorage({
            key: "code",
            data: e.code,
            success: function() {
                wx.getStorage({
                    key: "code",
                    success: function(e) {
                        console.log("存入code", e.data);
                    }
                });
            }
        });
    },
    getQQqueryRemain: function() {
        var e = this, t = {
            authToken: this.data.authToken,
            encryptPhone: this.data.encryptPhone,
            smsCode: ""
        };
        (t = JSON.parse(JSON.stringify(t))).smsCode = this.data.inputValue, console.log("query接口请求数据", t), 
        a.default.getQQqueryRemain(t).then(function(t) {
            console.log("query接口返回数据", t), e.setData({
                apiRes: t,
                showLoading: !1
            }), (t.data.flow >= 1024 || t.data.flow <= -1024) && (t.data.flowGB = (Number(t.data.flow) / 1024).toFixed(2)), 
            t.data.phone && (t.data.phone = e.formatPhoneNumber(t.data.phone)), e.setData({
                inputValue: "",
                inputList: JSON.parse(JSON.stringify(o)),
                userInfo: t.data
            }), e.putIntoStorage(t), "0" === t.code ? (clearTimeout(e.data.timer), e.setData({
                isLogin: !0,
                showModal: !1,
                showLoadIcon: !1,
                countDown: 0
            })) : (e.setData({
                isLogin: !1,
                showLoadIcon: !1
            }), "0005" === t.code ? (e.initShowModal(), clearTimeout(e.data.timer), e.setData({
                showModal: !0,
                countDown: 60
            }), e.tick()) : "0009" === t.code ? (e.setData({
                showModal: !0
            }), e.initShowModal(), wx.showToast({
                title: t.message,
                icon: "none"
            })) : "0015" === t.code ? e.setData({
                isLogin: !0,
                showModal: !1,
                showLoadIcon: !0,
                countDown: 0
            }) : (e.setData({
                countDown: 0
            }), clearTimeout(e.data.timer), wx.showToast({
                title: t.message,
                icon: "none"
            })));
        }).catch(function(t) {
            console.log("query接口失败", t), e.setData({
                showLoading: !1
            });
        });
    },
    tick: function() {
        var e = this, t = this.data, a = t.countDown, o = t.timer;
        a <= 0 ? (clearTimeout(o), this.setData({
            countDown: 0
        })) : this.data.timer = setTimeout(function() {
            e.setData({
                countDown: a - 1
            }), e.tick();
        }, 1e3);
    },
    initShowModal: function() {
        this.setData({
            showModal: !0,
            inputValue: "",
            inputList: JSON.parse(JSON.stringify(o))
        });
    },
    closeActionSheet: function() {
        this.setData({
            showModal: !1,
            inputValue: "",
            inputList: JSON.parse(JSON.stringify(o))
        });
    },
    inputHandler: function(e) {
        var t = e.detail.value;
        this.setData({
            inputValue: t
        }), this.changeData(), 6 === t.length && this.getQQqueryRemain();
    },
    changeData: function() {
        var e = this, a = String(this.data.inputValue).split("");
        this.data.inputList.forEach(function(o, n) {
            e.setData(t({}, "inputList[" + n + "].value", a[n] || "")), a.length > 0 ? n === a.length ? e.setData(t({}, "inputList[" + n + "].isFocus", !0)) : e.setData(t({}, "inputList[" + n + "].isFocus", !1)) : e.setData(t({
                "inputList[0].isFocus": !0
            }, "inputList[" + n + "].isFocus", !1));
        });
    },
    showTips: function() {
        this.setData({
            showIconInfo: !0
        });
    },
    closeTips: function() {
        this.setData({
            showIconInfo: !1
        });
    },
    formatPhoneNumber: function(e) {
        return e = (e = e.replace(/\s/g, "")).replace(/(\d{3})\*\*\*\*(\d{4})/, "$1 **** $2");
    },
    refresh: function() {
        this.setData({
            showLoading: !0
        }), this.getQQqueryRemain();
    },
    onShow: function() {
        wx.hideHomeButton(), wx.hideTabBar({
            animation: !0,
            success: function() {},
            fail: function() {}
        });
    },
    toHF: function() {
        wx.navigateToMiniProgram({
            appId: "wxad3150031786d672",
            path: "pages/main?entrance=LTWKYECX",
            envVersion: "release"
        });
    },
    toLL: function() {
        wx.navigateToMiniProgram({
            appId: "wxad3150031786d672",
            path: "traffic/pages/main?channel=LTWKYECX&extend=LTWKYECX&reload=1X",
            envVersion: "release"
        });
    }
});